//
//  UIVIewController+Animation.m
//  BanBu
//
//  Created by banbu3 on 10/21/13.
//
//

#import "UIVIewController+Animation.h"

@implementation UIViewController (UIViewControllerAnimation)


-(void)presentViewController:(UIViewController *)modalViewController animated:(BOOL)animated
{
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateStyle:NSDateFormatterMediumStyle];
    [dateFormatter setTimeStyle:NSDateFormatterShortStyle];
    [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss.SSSSSS"];
    if(IS_IOS_7&&modalViewController.navigationController)
    {
        if(animated)
        {
            UIImageView *imView = [[[UIImageView alloc] initWithImage:[self capture]] autorelease];
            imView.frame = CGRectMake(0, 0, __MainScreen_Width, __MainScreen_Height+20);
            [self.view.window addSubview:imView];
            [self.view.window insertSubview:imView atIndex:0];
            
            CATransition *animation = [CATransition animation];
            [animation setDuration:0.5];
            [animation setType:kCATransitionMoveIn]; //淡入淡出
            [animation setSubtype:kCATransitionFromTop];
//            self.view.window.backgroundColor = [UIColor colorWithPatternImage:[self capture]];
            [animation setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionDefault]];
            [modalViewController.view.layer addAnimation:animation forKey:nil];
            [self performSelector:@selector(setBackGroundColor:) withObject:imView afterDelay:0.6];
            [self presentViewController:modalViewController animated:NO completion:^{
            }];
            /* The identity transform: [1 0 0 0; 0 1 0 0; 0 0 1 0; 0 0 0 1]. */
            /* The identity transform: [0 1 0 0; 0 0 1 0; 0 0 0 1]. */
            /* The identity transform: [0 0 1 0; 0 0 0 1]. */
            /* The identity transform: [0 0 0 1]. */


        }
        else
        {
            [self presentViewController:modalViewController animated:NO completion:^{
                
            }];
        }
    }
    else
    {
        [self presentViewController:modalViewController animated:animated completion:nil];
    }

    [dateFormatter release];

}


-(void)setBackGroundColor:(UIView *)_aview
{
    [self performSelectorOnMainThread:@selector(setWindowColor:) withObject:_aview waitUntilDone:YES];
}
-(void)setWindowColor:(UIView *)_aview
{
    [_aview removeFromSuperview];
//    _aview.backgroundColor = [UIColor whiteColor];
}
-(UIImage *)capture
{
    UIGraphicsBeginImageContextWithOptions(CGSizeMake(__MainScreen_Width, __MainScreen_Height+20), self.view.opaque, 0.0);
    [self.navigationController.view.layer renderInContext:UIGraphicsGetCurrentContext()];
    UIImage *img = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return img;
}

-(void)dismissViewControllerAnimated:(BOOL)animated
{
    if(self.navigationController.viewControllers.count&&IS_IOS_7)
    {
        if(animated)
        {
            UIImageView *imView = [[[UIImageView alloc] initWithImage:[self capture]] autorelease];
            imView.frame = CGRectMake(0, 0, __MainScreen_Width, __MainScreen_Height + 20);
            [self.view.window addSubview:imView];
            [self.view.window bringSubviewToFront:imView];
            
            CATransition *animation = [CATransition animation];
            [animation setDuration:0.5];
            [animation setType:kCATransitionReveal];
            [animation setSubtype:kCATransitionFromBottom];
            [animation setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionDefault]];
            [self.view.window.layer addAnimation:animation forKey:nil];
            [imView removeFromSuperview];
            
            [self dismissViewControllerAnimated:NO completion:nil];
        }
        else
        {
            [self dismissViewControllerAnimated:NO completion:nil];
        }
    }
    else
    {
        [self dismissViewControllerAnimated:animated completion:^{
        }];
    }
}
@end
